import os, pygame
from pygame.locals import *
from ControllerInterface import ControllerInterface

class Controller(ControllerInterface):
    """This class keeps track of all the button states that come in off the event queue"""
    def __init__(self):
    
        # Setup the button states and a few other functions used by all characters
        ControllerInterface.__init__(self)
        
        self.joypads = list()
        self.joypad = None
        self.currentpad = -1
        self.mostbuttons = -1
        
        # Flick through all the connected joypads and pick the one with the most buttons. Maintain a list of the others.
        pygame.joystick.init()
        if pygame.joystick.get_count() > 0:
            for joyid in range(pygame.joystick.get_count()):
                self.joypads.append(pygame.joystick.Joystick(joyid))
                self.joypads[joyid].init()
                if self.joypads[joyid].get_numbuttons() > self.mostbuttons:
                    self.mostbuttons = self.joypads[joyid].get_numbuttons()
                    self.joypad = self.joypads[joyid]
                    self.currentpad = joyid

        self.up_key = K_UP
        self.up_button = 10

        self.down_key = K_DOWN
        self.down_button = 11

        self.left_key = K_LEFT
        self.left_button = 12
        
        self.right_key = K_RIGHT
        self.right_button = 13
        
        self.run_key = K_LCTRL
        self.run_button = 3
        
        self.jump_key = K_LSHIFT
        self.jump_button = 0
        
        self.charge_key = K_c
        self.charge_button = 7
        
        self.punch_high_key = K_a
        self.punch_high_button = 4
        
        self.punch_low_key = K_z
        self.punch_low_button = 1
        
        self.kick_high_key = K_s
        self.kick_high_button = 5

        self.kick_low_key = K_x
        self.kick_low_button = 2
        
        self.kick_low_key = K_ESCAPE
        self.kick_low_button = 9
        
        cfgfile = open(os.path.join('data',"controls.txt"))
        str = cfgfile.read()
        str = str.split('\n')
        
        for line in str:
            setter = line.split('=')
            if setter[0] == "up":
                self.up_key = strtoconst(setter[1])
            elif setter[0] == "down":
                self.down_key = strtoconst(setter[1])
            elif setter[0] == "left":
                self.left_key = strtoconst(setter[1])
            elif setter[0] == "right":
                self.right_key = strtoconst(setter[1])
            elif setter[0] == "run":
                self.run_key = strtoconst(setter[1])
            elif setter[0] == "jump":
                self.jump_key = strtoconst(setter[1])
            elif setter[0] == "charge":
                self.charge_key = strtoconst(setter[1])
            elif setter[0] == "kick_high":
                self.kick_high_key = strtoconst(setter[1])
            elif setter[0] == "kick_low":
                self.kick_low_key = strtoconst(setter[1])
            elif setter[0] == "punch_high":
                self.punch_high_key = strtoconst(setter[1])
            elif setter[0] == "punch_low":
                self.punch_low_key = strtoconst(setter[1])
            elif setter[0] == "start":
                self.start_key = strtoconst(setter[1])
            elif setter[0] == "jup":
                self.up_button = int(setter[1])
            elif setter[0] == "jdown":
                self.down_button = int(setter[1])
            elif setter[0] == "jleft":
                self.left_button = int(setter[1])
            elif setter[0] == "jright":
                self.right_button = int(setter[1])
            elif setter[0] == "jrun":
                self.run_button = int(setter[1])
            elif setter[0] == "jjump":
                self.jump_button = int(setter[1])
            elif setter[0] == "jcharge":
                self.charge_button = int(setter[1])
            elif setter[0] == "jkick_high":
                self.kick_high_button = int(setter[1])
            elif setter[0] == "jkick_low":
                self.kick_low_button = int(setter[1])
            elif setter[0] == "jpunch_high":
                self.punch_high_button = int(setter[1])
            elif setter[0] == "jpunch_low":
                self.punch_low_button = int(setter[1])
            elif setter[0] == "jstart":
                self.start_button = int(setter[1])

    def update(self):
        ControllerInterface.update(self)
        
        # Handle Input Events
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                return
            elif event.type == KEYDOWN:
                self.keydown(event.key)
            elif event.type == KEYUP:
                self.keyup(event.key)
            elif event.type == JOYBUTTONDOWN:
                self.joydown(event.button)
            elif event.type == JOYBUTTONUP:
                self.joyup(event.button)

    def keydown(self, key):
        if key == self.left_key:
            self.left = 1
            self.left_press = 1
        elif key == self.right_key:
            self.right = 1
            self.right_press = 1
        elif key == self.up_key:
            self.up = 1
            self.up_press = 1
        elif key == self.down_key:
            self.down = 1
            self.down_press = 1
        elif key == self.run_key:
            self.run = 1
            self.run_press = 1
        elif key == self.jump_key:
            self.jump = 1
            self.jump_press = 1
        elif key == self.charge_key:
            self.charge = 1
            self.charge_press = 1
        elif key == self.punch_high_key:
            self.punch_high = 1
            self.punch_high_press = 1
        elif key == self.punch_low_key:
            self.punch_low = 1
            self.punch_low_press = 1
        elif key == self.kick_high_key:
            self.kick_high = 1
            self.kick_high_press = 1
        elif key == self.kick_low_key:
            self.kick_low = 1
            self.kick_low_press = 1
        elif key == self.start_key:
            self.start = 1
            self.start_press = 1

    def keyup(self, key):
        if key == self.left_key:
            self.left = 0
            self.left_release = 1
        elif key == self.right_key:
            self.right = 0
            self.right_release = 1
        elif key == self.up_key:
            self.up = 0
            self.up_release = 1
        elif key == self.down_key:
            self.down = 0
            self.down_release = 1
        elif key == self.run_key:
            self.run = 0
            self.run_release = 1
        elif key == self.jump_key:
            self.jump = 0
            self.jump_release = 1
        elif key == self.charge_key:
            self.charge = 0
            self.charge_release = 1
        elif key == self.punch_high_key:
            self.punch_high = 0
            self.punch_high_release = 1
        elif key == self.punch_low_key:
            self.punch_low = 0
            self.punch_low_release = 1
        elif key == self.kick_high_key:
            self.kick_high = 0
            self.kick_high_release = 1
        elif key == self.kick_low_key:
            self.kick_low = 0
            self.kick_low_release = 1
        elif key == self.start_key:
            self.start = 0
            self.start_release = 1

    def joydown(self, button):
        if button == self.left_button:
            self.left = 1
            self.left_press = 1
        elif button == self.right_button:
            self.right = 1
            self.right_press = 1
        elif button == self.up_button:
            self.up = 1
            self.up_press = 1
        elif button == self.down_button:
            self.down = 1
            self.down_press = 1
        elif button == self.run_button:
            self.run = 1
            self.run_press = 1
        elif button == self.jump_button:
            self.jump = 1
            self.jump_press = 1
        elif button == self.charge_button:
            self.charge = 1
            self.charge_press = 1
        elif button == self.punch_high_button:
            self.punch_high = 1
            self.punch_high_press = 1
        elif button == self.punch_low_button:
            self.punch_low = 1
            self.punch_low_press = 1
        elif button == self.kick_high_button:
            self.kick_high = 1
            self.kick_high_press = 1
        elif button == self.kick_low_button:
            self.kick_low = 1
            self.kick_low_press = 1
        elif button == self.start_button:
            self.start = 1
            self.start_press = 1
            
    def joyup(self, button):
        if button == self.left_button:
            self.left = 0
            self.left_release = 1
        elif button == self.right_button:
            self.right = 0
            self.right_release = 1
        elif button == self.up_button:
            self.up = 0
            self.up_release = 1
        elif button == self.down_button:
            self.down = 0
            self.down_release = 1
        elif button == self.run_button:
            self.run = 0
            self.run_release = 1
        elif button == self.jump_button:
            self.jump = 0
            self.jump_release = 1
        elif button == self.charge_button:
            self.charge = 0
            self.charge_release = 1
        elif button == self.punch_high_button:
            self.punch_high = 0
            self.punch_high_release = 1
        elif button == self.punch_low_button:
            self.punch_low = 0
            self.punch_low_release = 1
        elif button == self.kick_high_button:
            self.kick_high = 0
            self.kick_high_release = 1
        elif button == self.kick_low_button:
            self.kick_low = 0
            self.kick_low_release = 1
        elif button == self.start_button:
            self.start = 0
            self.start_release = 1
    
    def exportconfig(self):
        properties = list()
        properties.append(("up",consttostr(self.up_key)))
        properties.append(("down",consttostr(self.down_key)))
        properties.append(("left",consttostr(self.left_key)))
        properties.append(("right",consttostr(self.right_key)))
        properties.append(("run",consttostr(self.run_key)))
        properties.append(("jump",consttostr(self.jump_key)))
        properties.append(("charge",consttostr(self.charge_key)))
        properties.append(("kick_high",consttostr(self.kick_high_key)))
        properties.append(("kick_low",consttostr(self.kick_low_key)))
        properties.append(("punch_high",consttostr(self.punch_high_key)))
        properties.append(("punch_low",consttostr(self.punch_low_key)))
        properties.append(("start",str(self.start_key)))
        properties.append(("jup",str(self.up_button)))
        properties.append(("jdown",str(self.down_button)))
        properties.append(("jleft",str(self.left_button)))
        properties.append(("jright",str(self.right_button)))
        properties.append(("jrun",str(self.run_button)))
        properties.append(("jjump",str(self.jump_button)))
        properties.append(("jcharge",str(self.charge_button)))
        properties.append(("jkick_high",str(self.kick_high_button)))
        properties.append(("jkick_low",str(self.kick_low_button)))
        properties.append(("jpunch_high",str(self.punch_high_button)))
        properties.append(("jpunch_low",str(self.punch_low_button)))
        properties.append(("jstart",str(self.start_button)))
        return properties
    
def strtoconst(str):
    if str == "K_BACKSPACE":
        return K_BACKSPACE
    if str == "K_TAB":
        return K_TAB
    if str == "K_CLEAR":
        return K_CLEAR
    if str == "K_RETURN":
        return K_RETURN
    if str == "K_PAUSE":
        return K_PAUSE
    if str == "K_ESCAPE":
        return K_ESCAPE
    if str == "K_SPACE":
        return K_SPACE
    if str == "K_EXCLAIM":
        return K_EXCLAIM
    if str == "K_QUOTEDBL":
        return K_QUOTEDBL
    if str == "K_HASH":
        return K_HASH
    if str == "K_DOLLAR":
        return K_DOLLAR
    if str == "K_AMPERSAND":
        return K_AMPERSAND
    if str == "K_QUOTE":
        return K_QUOTE
    if str == "K_LEFTPAREN":
        return K_LEFTPAREN
    if str == "K_RIGHTPAREN":
        return K_RIGHTPAREN 
    if str == "K_ASTERISK":
        return K_ASTERISK
    if str == "K_PLUS":
        return K_PLUS
    if str == "K_COMMA":
        return K_COMMA
    if str == "K_MINUS":
        return K_MINUS
    if str == "K_PERIOD":
        return K_PERIOD
    if str == "K_SLASH":
        return K_SLASH
    if str == "K_0":
        return K_0
    if str == "K_1":
        return K_1
    if str == "K_2":
        return K_2
    if str == "K_3":
        return K_3
    if str == "K_4":
        return K_4
    if str == "K_5":
        return K_5
    if str == "K_6":
        return K_6
    if str == "K_7":
        return K_7
    if str == "K_8":
        return K_8
    if str == "K_9":
        return K_9
    if str == "K_COLON":
        return K_COLON
    if str == "K_SEMICOLON":
        return K_SEMICOLON
    if str == "K_LESS":
        return K_LESS
    if str == "K_EQUALS":
        return K_EQUALS
    if str == "K_GREATER":
        return K_GREATER
    if str == "K_QUESTION":
        return K_QUESTION
    if str == "K_AT":
        return K_AT
    if str == "K_LEFTBRACKET":
        return K_LEFTBRACKET
    if str == "K_BACKSLASH":
        return K_BACKSLASH
    if str == "K_RIGHTBRACKET":
        return K_RIGHTBRACKET 
    if str == "K_CARET":
        return K_CARET
    if str == "K_UNDERSCORE":
        return K_UNDERSCORE
    if str == "K_BACKQUOTE":
        return K_BACKQUOTE
    if str == "K_a":
        return K_a
    if str == "K_b":
        return K_b
    if str == "K_c":
        return K_c
    if str == "K_d":
        return K_d
    if str == "K_e":
        return K_e
    if str == "K_f":
        return K_f
    if str == "K_g":
        return K_g
    if str == "K_h":
        return K_h
    if str == "K_i":
        return K_i
    if str == "K_j":
        return K_j
    if str == "K_k":
        return K_k
    if str == "K_l":
        return K_l
    if str == "K_m":
        return K_m
    if str == "K_n":
        return K_n
    if str == "K_o":
        return K_o
    if str == "K_p":
        return K_p
    if str == "K_q":
        return K_q
    if str == "K_r":
        return K_r
    if str == "K_s":
        return K_s
    if str == "K_t":
        return K_t
    if str == "K_u":
        return K_u
    if str == "K_v":
        return K_v
    if str == "K_w":
        return K_w
    if str == "K_x":
        return K_x
    if str == "K_y":
        return K_y
    if str == "K_z":
        return K_z
    if str == "K_DELETE":
        return K_DELETE
    if str == "K_KP0":
        return K_KP0
    if str == "K_KP1":
        return K_KP1
    if str == "K_KP2":
        return K_KP2
    if str == "K_KP3":
        return K_KP3
    if str == "K_KP4":
        return K_KP4
    if str == "K_KP5":
        return K_KP5
    if str == "K_KP6":
        return K_KP6
    if str == "K_KP7":
        return K_KP7
    if str == "K_KP8":
        return K_KP8
    if str == "K_KP9":
        return K_KP9
    if str == "K_KP_PERIOD":
        return K_KP_PERIOD
    if str == "K_KP_DIVIDE":
        return K_KP_DIVIDE
    if str == "K_KP_MULTIPLY":
        return K_KP_MULTIPLY
    if str == "K_KP_MINUS":
        return K_KP_MINUS
    if str == "K_KP_PLUS":
        return K_KP_PLUS
    if str == "K_KP_ENTER":
        return K_KP_ENTER
    if str == "K_KP_EQUALS":
        return K_KP_EQUALS
    if str == "K_UP":
        return K_UP
    if str == "K_DOWN":
        return K_DOWN
    if str == "K_RIGHT":
        return K_RIGHT
    if str == "K_LEFT":
        return K_LEFT
    if str == "K_INSERT":
        return K_INSERT
    if str == "K_HOME":
        return K_HOME
    if str == "K_END":
        return K_END
    if str == "K_PAGEUP":
        return K_PAGEUP
    if str == "K_PAGEDOWN":
        return K_PAGEDOWN
    if str == "K_F1":
        return K_F1
    if str == "K_F2":
        return K_F2
    if str == "K_F3":
        return K_F3
    if str == "K_F4":
        return K_F4
    if str == "K_F5":
        return K_F5
    if str == "K_F6":
        return K_F6
    if str == "K_F7":
        return K_F7
    if str == "K_F8":
        return K_F8
    if str == "K_F9":
        return K_F9
    if str == "K_F10":
        return K_F10
    if str == "K_F11":
        return K_F11
    if str == "K_F12":
        return K_F12
    if str == "K_F13":
        return K_F13
    if str == "K_F14":
        return K_F14
    if str == "K_F15":
        return K_F15
    if str == "K_NUMLOCK":
        return K_NUMLOCK
    if str == "K_CAPSLOCK":
        return K_CAPSLOCK
    if str == "K_SCROLLOCK":
        return K_SCROLLOCK
    if str == "K_RSHIFT":
        return K_RSHIFT
    if str == "K_LSHIFT":
        return K_LSHIFT
    if str == "K_RCTRL":
        return K_RCTRL
    if str == "K_LCTRL":
        return K_LCTRL
    if str == "K_RALT":
        return K_RALT
    if str == "K_LALT":
        return K_LALT
    if str == "K_RMETA":
        return K_RMETA
    if str == "K_LMETA":
        return K_LMETA
    if str == "K_LSUPER":
        return K_LSUPER
    if str == "K_RSUPER":
        return K_RSUPER
    if str == "K_MODE":
        return K_MODE
    if str == "K_HELP":
        return K_HELP
    if str == "K_PRINT":
        return K_PRINT
    if str == "K_SYSREQ":
        return K_SYSREQ
    if str == "K_BREAK":
        return K_BREAK
    if str == "K_MENU":
        return K_MENU
    if str == "K_POWER":
        return K_POWER
    if str == "K_EURO":
        return K_EURO
        
def consttostr(str):
    if str == K_BACKSPACE:
        return "K_BACKSPACE"
    if str == K_TAB:
        return "K_TAB"
    if str == K_CLEAR:
        return "K_CLEAR"
    if str == K_RETURN:
        return "K_RETURN"
    if str == K_PAUSE:
        return "K_PAUSE"
    if str == K_ESCAPE:
        return "K_ESCAPE"
    if str == K_SPACE:
        return "K_SPACE"
    if str == K_EXCLAIM:
        return "K_EXCLAIM"
    if str == K_QUOTEDBL:
        return "K_QUOTEDBL"
    if str == K_HASH:
        return "K_HASH"
    if str == K_DOLLAR:
        return "K_DOLLAR"
    if str == K_AMPERSAND:
        return "K_AMPERSAND"
    if str == K_QUOTE:
        return "K_QUOTE"
    if str == K_LEFTPAREN:
        return "K_LEFTPAREN"
    if str == K_RIGHTPAREN:
        return "K_RIGHTPAREN"
    if str == K_ASTERISK:
        return "K_ASTERISK"
    if str == K_PLUS:
        return "K_PLUS"
    if str == K_COMMA:
        return "K_COMMA"
    if str == K_MINUS:
        return "K_MINUS"
    if str == K_PERIOD:
        return "K_PERIOD"
    if str == K_SLASH:
        return "K_SLASH"
    if str == K_0:
        return "K_0"
    if str == K_1:
        return "K_1"
    if str == K_2:
        return "K_2"
    if str == K_3:
        return "K_3"
    if str == K_4:
        return "K_4"
    if str == K_5:
        return "K_5"
    if str == K_6:
        return "K_6"
    if str == K_7:
        return "K_7"
    if str == K_8:
        return "K_8"
    if str == K_9:
        return "K_9"
    if str == K_COLON:
        return "K_COLON"
    if str == K_SEMICOLON:
        return "K_SEMICOLON"
    if str == K_LESS:
        return "K_LESS"
    if str == K_EQUALS:
        return "K_EQUALS"
    if str == K_GREATER:
        return "K_GREATER"
    if str == K_QUESTION:
        return "K_QUESTION"
    if str == K_AT:
        return "K_AT"
    if str == K_LEFTBRACKET:
        return "K_LEFTBRACKET"
    if str == K_BACKSLASH:
        return "K_BACKSLASH"
    if str == K_RIGHTBRACKET:
        return "K_RIGHTBRACKET"
    if str == K_CARET:
        return "K_CARET"
    if str == K_UNDERSCORE:
        return "K_UNDERSCORE"
    if str == K_BACKQUOTE:
        return "K_BACKQUOTE"
    if str == K_a:
        return "K_a"
    if str == K_b:
        return "K_b"
    if str == K_c:
        return "K_c"
    if str == K_d:
        return "K_d"
    if str == K_e:
        return "K_e"
    if str == K_f:
        return "K_f"
    if str == K_g:
        return "K_g"
    if str == K_h:
        return "K_h"
    if str == K_i:
        return "K_i"
    if str == K_j:
        return "K_j"
    if str == K_k:
        return "K_k"
    if str == K_l:
        return "K_l"
    if str == K_m:
        return "K_m"
    if str == K_n:
        return "K_n"
    if str == K_o:
        return "K_o"
    if str == K_p:
        return "K_p"
    if str == K_q:
        return "K_q"
    if str == K_r:
        return "K_r"
    if str == K_s:
        return "K_s"
    if str == K_t:
        return "K_t"
    if str == K_u:
        return "K_u"
    if str == K_v:
        return "K_v"
    if str == K_w:
        return "K_w"
    if str == K_x:
        return "K_x"
    if str == K_y:
        return "K_y"
    if str == K_z:
        return "K_z"
    if str == K_DELETE:
        return "K_DELETE"
    if str == K_KP0:
        return "K_KP0"
    if str == K_KP1:
        return "K_KP1"
    if str == K_KP2:
        return "K_KP2"
    if str == K_KP3:
        return "K_KP3"
    if str == K_KP4:
        return "K_KP4"
    if str == K_KP5:
        return "K_KP5"
    if str == K_KP6:
        return "K_KP6"
    if str == K_KP7:
        return "K_KP7"
    if str == K_KP8:
        return "K_KP8"
    if str == K_KP9:
        return "K_KP9"
    if str == K_KP_PERIOD:
        return "K_KP_PERIOD"
    if str == K_KP_DIVIDE:
        return "K_KP_DIVIDE"
    if str == K_KP_MULTIPLY:
        return "K_KP_MULTIPLY"
    if str == K_KP_MINUS:
        return "K_KP_MINUS"
    if str == K_KP_PLUS:
        return "K_KP_PLUS"
    if str == K_KP_ENTER:
        return "K_KP_ENTER"
    if str == K_KP_EQUALS:
        return "K_KP_EQUALS"
    if str == K_UP:
        return "K_UP"
    if str == K_DOWN:
        return "K_DOWN"
    if str == K_RIGHT:
        return "K_RIGHT"
    if str == K_LEFT:
        return "K_LEFT"
    if str == K_INSERT:
        return "K_INSERT"
    if str == K_HOME:
        return "K_HOME"
    if str == K_END:
        return "K_END"
    if str == K_PAGEUP:
        return "K_PAGEUP"
    if str == K_PAGEDOWN:
        return "K_PAGEDOWN"
    if str == K_F1:
        return "K_F1"
    if str == K_F2:
        return "K_F2"
    if str == K_F3:
        return "K_F3"
    if str == K_F4:
        return "K_F4"
    if str == K_F5:
        return "K_F5"
    if str == K_F6:
        return "K_F6"
    if str == K_F7:
        return "K_F7"
    if str == K_F8:
        return "K_F8"
    if str == K_F9:
        return "K_F9"
    if str == K_F10:
        return "K_F10"
    if str == K_F11:
        return "K_F11"
    if str == K_F12:
        return "K_F12"
    if str == K_F13:
        return "K_F13"
    if str == K_F14:
        return "K_F14"
    if str == K_F15:
        return "K_F15"
    if str == K_NUMLOCK:
        return "K_NUMLOCK"
    if str == K_CAPSLOCK:
        return "K_CAPSLOCK"
    if str == K_SCROLLOCK:
        return "K_SCROLLOCK"
    if str == K_RSHIFT:
        return "K_RSHIFT"
    if str == K_LSHIFT:
        return "K_LSHIFT"
    if str == K_RCTRL:
        return "K_RCTRL"
    if str == K_LCTRL:
        return "K_LCTRL"
    if str == K_RALT:
        return "K_RALT"
    if str == K_LALT:
        return "K_LALT"
    if str == K_RMETA:
        return "K_RMETA"
    if str == K_LMETA:
        return "K_LMETA"
    if str == K_LSUPER:
        return "K_LSUPER"
    if str == K_RSUPER:
        return "K_RSUPER"
    if str == K_MODE:
        return "K_MODE"
    if str == K_HELP:
        return "K_HELP"
    if str == K_PRINT:
        return "K_PRINT"
    if str == K_SYSREQ:
        return "K_SYSREQ"
    if str == K_BREAK:
        return "K_BREAK"
    if str == K_MENU:
        return "K_MENU"
    if str == K_POWER:
        return "K_POWER"
    if str == K_EURO:
        return "K_EURO"
